package org.hive.vmware.translator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hive.vmware.client.model.VmGroupVO;
import org.hive.vmware.client.model.VmVO;
import org.hive.vmware.model.VmBee;
import org.hive.vmware.model.VmGroupBee;

public class VmGroupModelVOTranslator {

	public List<VmGroupVO> translateVmGroupsToVO(List<VmGroupBee> vmGroupBees){
		if (vmGroupBees == null) return null;
		List<VmGroupVO> result = new ArrayList<VmGroupVO>();
		for (VmGroupBee vmGroupBee : vmGroupBees) {
			result.add(translateVmGroupToVO(vmGroupBee));
		}
		return result; 
	}
	
	public List<VmGroupBee> translateVmGroupsToModel(List<VmGroupVO> vmGroups) {
		if (vmGroups == null) return null;
		List<VmGroupBee> vmGroupBees = new ArrayList<VmGroupBee>();
		for (VmGroupVO vmGroup : vmGroups) {
			vmGroupBees.add(translateVmGroupToModel(vmGroup));
		}
		
		return vmGroupBees;
	}
	
	VmGroupBee translateVmGroupToModel(VmGroupVO vmGroupVO) {
		VmGroupBee groupBee = new VmGroupBee();
		groupBee.setId(vmGroupVO.getName());
		List<VmVO> vms = vmGroupVO.getVms();
		for (VmVO vmVO : vms) {
			groupBee.addVm(translateVmToModel(vmVO));
		}
		return groupBee;
	}
	
	VmBee translateVmToModel(VmVO vmVO) {
		VmBee vm = new VmBee();
		vm.setName(vmVO.getName());
		vm.setHost(vmVO.getHost());
		vm.setDataCenter(vmVO.getDataCenter());
		vm.setCenterServer(vmVO.getCenterServer());
		if (vmVO.getVmGroupVO() != null) {
			vm.setVmGroup(translateVmGroupToModel(vmVO.getVmGroupVO()));
		}
		return vm;
		
	}
	
	VmGroupVO translateVmGroupToVO(VmGroupBee vmGroupBee) {
		VmGroupVO vmGroupVO = new VmGroupVO();
		vmGroupVO.setName(vmGroupBee.getId());
		List<VmVO> vmVOs = new ArrayList<VmVO>();
		Set<VmBee> vms = vmGroupBee.getVms();
		Iterator<VmBee> vmBeeIterator = vms.iterator();
		while(vmBeeIterator.hasNext()) {
			vmVOs.add(translateVmToVO(vmBeeIterator.next()));
		}
		
		vmGroupVO.setVms(vmVOs);
		return vmGroupVO;
	}
	
	VmVO translateVmToVO(VmBee vm) {
		VmVO vmVO = new VmVO();
		vmVO.setName(vm.getName());
		vmVO.setDataCenter(vm.getDataCenter());
		vmVO.setCenterServer(vm.getCenterServer());
		vmVO.setHost(vm.getHost());
		if (vm.getVmGroup() != null) { 
			vmVO.setVmGroupVO(translateVmGroupToVO(vm.getVmGroup()));
		}
		vmVO.setDescription(vm.getName() + "/" + vm.getHost());
		return vmVO;
	}
	
	
}
